/**
 * @format
 * @Author: TC
 * @Date: 2024-05-03 13:36:09
 * @Last Modified by: TC
 * @Last Modified time: 2025-06-08 11:11:53
 */

import { color, Color, Material, Node, sp, Sprite, Tween, tween, UIRenderer, v2, v3, Vec3 } from "cc";
import { app } from "db://assets/FrameWork/App";
import { nsCmdConst } from "db://assets/FrameWork/Module/InputCmd/CmdConst";
import { ICmdData } from "../../../../FrameWork/Module/InputCmd/CmdInterface";
import { nsBattleConst } from "../../Battle/BattleConst";
import { nsEntityCosnt } from "../Const/EntityConst";
import { EntityBase } from "../EntityBase";

export class EntityAction {
    protected _whitePercent: number = 0;
    protected _mainMtl: Material = null;
    public firing = false;
    private _bLeft = false;

    private _nodeMc: Node = null;
    public get nodeMc(): Node {
        return this._nodeMc;
    }

    //spine
    private _ske: sp.Skeleton = null;
    public get ske(): sp.Skeleton {
        return this._ske;
    }
    private _jumpTimes = 0; //连跳次数
    public set jumpTimes(j: number) {
        this._jumpTimes = j;
    }
    public get jumpTimes(): number {
        return this._jumpTimes;
    }

    private _hitGroudNum = 0;
    public set hitGroudNum(h: number) {
        this._hitGroudNum = h;
    }
    public get hitGroudNum(): number {
        return this._hitGroudNum;
    }
    private _entity: EntityBase = null;
    public get entity(): EntityBase {
        return this._entity;
    }

    private _scaleInit = 1;
    private _scale: Vec3 = v3(1, 1, 1);
    public get scale(): Vec3 {
        return this._scale;
    }

    constructor(entity: EntityBase) {
        this._entity = entity;
        this._nodeMc = this._entity.node.getChildByName("nodeSp");
        this._ske = this._nodeMc.getChildByName("ske").getComponent(sp.Skeleton);
        this._scaleInit = Math.abs(this._nodeMc.scale.x);
        this._boneGun = this._ske.findBone("Gun");
        this._changeCustomMaterial();
    }

    /**
     * 修改自定义材质球
     */
    private _changeCustomMaterial(): void {
        // if (this._nodeMc.getComponent(Sprite)) {
        //     app.shaderMgr.changeMaterial(this._nodeMc.getComponent(UIRenderer), "2D/Custom/custom2D", () => {
        //         // this._mainMtl = this._nodeMc.getComponent(UIRenderer).getMaterialInstance(0);
        //     });
        // } else {
        //     app.shaderMgr.changeMaterial(this._nodeMc.getComponent(UIRenderer), "2D/CustomSpine/customSpine", () => {
        //         // this._mainMtl = this._nodeMc.getComponent(sp.Skeleton)["getMaterialForBlendAndTint"](2, 4, 0);
        //     });
        // }
    }

    protected _updateMtl(): void {
        if (this._nodeMc.getComponent(Sprite)) {
            this._mainMtl = this._nodeMc.getComponent(UIRenderer).getMaterialInstance(0);
        } else {
            this._mainMtl = this._nodeMc.getComponent(sp.Skeleton)["getMaterialForBlendAndTint"](2, 4, 0);
        }
    }

    /**
     * 待机
     */
    public doIdle(): void {}

    /**
     * 死亡
     */
    public doDie(): void {}

    /**
     * 攻击
     */
    public doAttack(cmd: ICmdData): void {}

    /**
     * 技能
     * @param skillIndex 技能索引
     */
    public doSkill(skillIndex: number): void {}

    /**
     * 受伤
     */
    public doHit(): void {
        // this.entity.animator.doHit();
        this._blinkWhite();
    }

    /**
     * 闪白
     */
    protected _blinkWhite(): void {
        this._whitePercent = 1;
    }

    /**
     * 隐藏
     */
    public hide(): void {
        this.nodeMc.getComponent(UIRenderer).color = color(255, 255, 255, 0);
        // this.entity.hpBar.node.active = false;
    }

    /**
     * 显示
     */
    public show(): void {
        this.nodeMc.getComponent(UIRenderer).color = Color.WHITE;
        // this.entity.hpBar.node.active = true;
    }

    public tick(): void {
        if (0 == this._whitePercent) {
            return;
        }
        this._whitePercent -= 0.05;
        if (this._whitePercent <= 0) {
            this._whitePercent = 0;
        }
        this._updateMtl();
        // if (this._nodeMc.getComponent(Sprite)) {
        // 	app.shaderMgr.setNumberProperty(
        // 		this._nodeMc.getComponent(Sprite).getMaterialInstance(0),
        // 		"whitePercent",
        // 		this._whitePercent
        // 	);
        // } else {
        // 	app.shaderMgr.setNumberProperty(
        // 		this._nodeMc.getComponent(sp.Skeleton)["getMaterialForBlendAndTint"](2, 4, 0),
        // 		"whitePercent",
        // 		this._whitePercent
        // 	);
        // }
        // app.shaderMgr.setNumberProperty(this._mainMtl, "whitePercent", this._whitePercent);
    }

    //#region 攻击相关
    //是否可攻击
    private _canShoot: boolean = true;
    //武器骨骼
    protected _boneGun: sp.spine.Bone;
    public get boneGun(): sp.spine.Bone {
        return this._boneGun;
    }
    //正在攻击
    public firting = false;

    /**
     * 刷新射击角度
     * @param angle
     */
    public updateFireAngle(angle: number): void {
        // console.log(Math.abs(angle));
        if (Math.abs(angle) < 90) {
            this._boneGun.data.rotation = angle;
        } else {
            this._boneGun.data.rotation = 180 - angle;
        }
    }

    /**
     * 移动
     * @param angle
     */
    public doMove(angle: number): void {
        // console.log("move  111", angle);
        this.entity.speed.setAngle(angle);
    }

    /**
     * 子弹
     */
    public shoot(): void {
        console.log("shoot  111");
    }
}
